package com.intct.hbase.dml;

import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Table;

import java.io.IOException;

/**
 * @author gufg
 * @since 2025-09-29 09:54
 */
public abstract class AbsDMLSerivce {
    // 实现方法
    public void start(String namespace, String tName) throws IOException {
        // 1 创建HBase
        Connection connection = ConnectionFactory.createConnection();

        // 2 获取Admin实例
        Admin admin = connection.getAdmin();

        // 获取Table实例
        TableName tableName = TableName.valueOf(namespace, tName);
        Table table = connection.getTable(tableName);


        // 3 判断是否存在
        if (admin.tableExists(tableName)) {
            // 3.1 创建/删除 。。。业务
            execCommand(table);
        }

        // 4 关闭资源
        table.close();
        admin.close();
        connection.close();
    }

    // 抽象方法
    public abstract void execCommand(Table table);
}
